//  网络请求配置
import axios from "axios";
import { message } from "antd";
import Qs from "qs";

axios.defaults.timeout = 15000;

//  axios.defaults.baseURL = "";

//    http request  请求拦截器
axios.interceptors.request.use(
  (config) => {
    config.data = JSON.stringify(config.data);
    config.headers = {
      "Content-Type": "charset=UTF-8;multipart/form-data;",
      Accept: "application/json, text/plain, */*",
    };
    if (localStorage.getItem("Authorization")) {
      config.headers.Authorization = localStorage.getItem("Authorization");
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

/**
 * http response 响应拦截器
 */
//响应拦截器即异常处理
axios.interceptors.response.use(
  (response) => {
    if (response.data.errCode === 2) {
      console.log("过期");
    }
    return response;
  },
  (error) => {
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          error.message = "错误请求";
          break;
        case 401:
          error.message = "未授权，请重新登录";
          window.location.href = "/login";
          break;
        case 403:
          error.message = "拒绝访问";
          break;
        case 404:
          error.message = "请求错误,未找到该资源";
          break;
        case 405:
          error.message = "请求方法未允许";
          break;
        case 408:
          error.message = "请求超时";
          break;
        case 500:
          error.message = "服务器端出错";
          break;
        case 501:
          error.message = "网络未实现";
          break;
        case 502:
          error.message = "网络错误";
          break;
        case 503:
          error.message = "服务不可用 您无权限查看";
          window.location.href = "/503";
          break;
        case 504:
          error.message = "网络超时或服务端出错!";
          break;
        case 505:
          error.message = "http版本不支持该请求";
          break;
        default:
          error.message = `连接错误${error.response.status}`;
      }
    } else {
      error.message = "连接到服务器失败";
    }

    message.error(error.message);
    return Promise.resolve(error.response);
  }
);

/**
 * 封装get方法
 * @param url  请求url
 * @param params  请求参数
 * @returns {Promise}
 */
export function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: params,
      })
      .then((response) => {
        // landing(url, params, response.data);
        resolve(response.data);
      })
      .catch((error) => {
        reject(error);
      });
  });
}

/**
 * 封装post2 参数放到请求头
 *  @param url
 * @param data
 * @returns {Promise}
 */

export function post2(url, data) {
  return new Promise((resolve, reject) => {
    axios
      .post(url + "?" + Qs.stringify(data))
      .then((response) => {
        resolve(response.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

const instance = axios.create({
  timeout: 15000,
  headers: {
    "X-Custom-Header": "foobar",
    "Content-Type":
      "application/x-www-form-urlencoded;charset=UTF-8;multipart/form-data",
    Authorization: localStorage.getItem("Authorization"),
  },
});

export function post(url, data) {
  return new Promise((resolve, reject) => {
    instance
      .post(url, Qs.stringify(data))
      .then((response) => {
        resolve(response.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function patch(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.patch(url, data).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        // msag(err);
        reject(err);
      }
    );
  });
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.put(url, data).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        // msag(err);
        reject(err);
      }
    );
  });
}

//统一接口处理，返回数据
export function htp(fecth, url, param) {
  let _data = "";
  return new Promise((resolve, reject) => {
    switch (fecth) {
      case "get":
        console.log("begin a get request,and url:", url);
        axios
          .get(url, param)
          .then(function (response) {
            resolve(response.data);
          })
          .catch(function (error) {
            console.log("get request GET failed.", error);
            reject(error);
          });
        break;
      case "post":
        axios
          .post(url, param)
          .then(function (response) {
            resolve(response);
          })
          .catch(function (error) {
            console.log("get request POST failed.", error);
            reject(error);
          });
        break;
      default:
        break;
    }
  });
}

// module.exports = function (app) {
//   app.use(
//     "/platform",
//     proxy({
//       target: "", //后台服务器地址
//       changeOrigin: true,
//       pathRewrite: {
//         "^/api": "",
//       },
//     })
//   );
// };

/**
 * 查看返回的数据
 * @param url
 * @param params
 * @param data
 */
function landing(url, params, data) {
  if (data.code === -1) {
  }
}
